import time
from elasticsearch import Elasticsearch
from redis import StrictRedis

es = Elasticsearch([{'host': '121.4.140.74', 'port': 9200}], timeout=3600)
redis = StrictRedis(host='121.4.140.74', port=2333, db=1, password='snbzsyc', decode_responses=True)
start = time.time()
while True:
    for ip in redis.keys():
        count = int(redis.get(ip))
        if count>4:
            count=4
        action = {
                "ip": ip,
                "count": count,
                "@timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                 "time": int(round(time.time() * 1000))
            }
        res = es.index(index="index_b", doc_type="doc_type_test", body=action)
        print(res)
    time.sleep(1)
    end = time.time()
    if end - start > 10:
        for elem in redis.keys():
            redis.delete(elem)
            start = end



